﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iacym.Entities;
using System.Data;
//
using System.Data.Objects;

namespace Iacym.Repository
{
    public class LibroRespository : MasterRepository, ILibroRespository
    {
        public List<LIBRO> GetAllFromLibros()
        {
            var query = from p in context.LIBROes
                        select p;
            ObjectContext.Refresh(RefreshMode.StoreWins, query);
            return query.ToList();
        }

        public List<LIBRO> GetAllFromLibrosByDescripcion(string criterio)
        {
            if (criterio == string.Empty)
            {
                var query = from p in context.LIBROes
                            select p;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
            else
            {
                var query = from p in context.LIBROes
                            where p.Descripcion.ToUpper().Contains(criterio.ToUpper())
                            select p;
                ObjectContext.Refresh(RefreshMode.StoreWins, query);
                
                return query.ToList();
            }
        }

        public LIBRO GetLibroById(int id)
        {
            var query = from p in context.LIBROes
                        where p.Id.Equals(id)
                        select p;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.SingleOrDefault();
        }

        public void AddLibro(LIBRO libro)
        {
            //context.Entry(libro.LIBRO_IMAGEN).State = EntityState.Unchanged;
            //context.Entry(libro).State = EntityState.Added;

            context.LIBROes.Add(libro);
            context.SaveChanges();
        }

        public void UpdateLibro(LIBRO libro)
        {
            context.Entry(libro.LIBRO_IMAGEN).State = EntityState.Modified;
            context.Entry(libro).State = EntityState.Modified;
            context.SaveChanges();
        }

        public void DeleteLibro(int id)
        {
            var existe = context.LIBROes.Where(p => p.Id.Equals(id)).SingleOrDefault();

            if (existe != null)
            {
                var libroimg = context.LIBRO_IMAGEN.Where(c => c.LibroId.Equals(id)).SingleOrDefault();
                context.LIBROes.Remove(existe);
                context.LIBRO_IMAGEN.Remove(libroimg);
                context.SaveChanges();
            }
        }

        public bool ExistDuplicidadDescripcion(string descripcion, int id)
        {
            var query = from p in context.LIBROes
                        where p.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0 && p.Id != id
                        select p;

            return query.Any();
        }


        public bool ExistImageHash(string hash, int id)
        {
            var query = from p in context.LIBRO_IMAGEN
                        where p.Hash.ToUpper().CompareTo(hash.ToUpper()) == 0 && p.LibroId != id
                        select p;

            return query.Any();
        }
    }
}
